<?php

namespace app\webapi\controller;

use think\facade\Db;
use think\facade\Request;

class ApiAddress extends ApiBaseController
{
    public $user_info = null;

    public function __construct()
    {
        $this->user_info = $this->checkToken();
        if (!preg_match('/^\d+$/', $this->user_info['id'])) {
            $this->result_error('用户id出错');
        }
    }

    /** 获取收货地址 */
    public function getList()
    {
        $user_info = $this->user_info;
        $sql = "SELECT * FROM sy_user_address WHERE uid=? ORDER BY isdefault DESC,uid ASC ";
        $sql_result = Db::query($sql, [$user_info['id']]);
        $this->result_ok("ok", $sql_result);
    }

    /** 获取收货详情 */
    public function getDetail()
    {
        $user_info = $this->user_info;
        $id = Request::param('id');
        if (!($id > 0)) {
            $this->result_error('收货信息id错误!', $id);
        }
        $sql = "SELECT * FROM user_address WHERE uid=? AND id=? ";
        $sql_result = Db::name('user_address')->where('id', $id)->find();
        if (!empty($sql_result)) {
            $this->result_ok("ok", $sql_result);
        }
        $this->result_error('没有数据!', $id);

    }

    /** 添加收货地址 */
    public function add()
    {
        $user_info = $this->user_info;
        $name = Request::param('name');
        $phone = Request::param('phone');
        $province = Request::param('province');
        $city = Request::param('city');
        $county = Request::param('county');
        $detail = Request::param('detail');
        $isdefault = (int)Request::param('isdefault');

        if (empty($name)) {
            $this->result_error("收件人不能为空");
        }
        if (empty($phone)) {
            $this->result_error("手机号不能为空");
        }
        if (empty($province)) {
            $this->result_error("省份不能为空");
        }
        if (empty($city)) {
            $this->result_error("城市不能为空");
        }
        if (empty($county)) {
            $this->result_error("市、县(区)不能为空");
        }
        if (empty($detail)) {
            $this->result_error("地址详情不能为空");
        }

        if (!preg_match('/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/', $phone)) {
            $this->result_error('手机号格式错误');
        }

        if (!(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $name) && strlen($name) < 25)) {
            $this->result_error('姓名错误');
        }
        if (empty($detail) || strlen($detail) > 60) {
            $this->result_error('详细地址错误');
        }
        $isdefault = $isdefault ? 1 : 0;
        if ($isdefault) {

            //UPDATE user_address SET isdefault=0 WHERE uid=? AND isdefault=1
            Db::name('user_address')->where('uid', $user_info['id'])->where('isdefault', 1)
                ->update(['isdefault' => 0]);

        }

        $in_res = Db::name('user_address')->insertGetId([
            'uid' => $user_info['id'],
            'name' => $name,
            'phone' => $phone,
            'province' => $province,
            'city' => $city,
            'county' => $county,
            'detail' => $detail,
            'isdefault' => $isdefault
        ]);
        if ($in_res) {
            $this->updateUserInfo($user_info['id']);
            $this->result_ok("添加成功");
        }
        $this->result_error("添加失败");
    }

    /** 修改收货地址 */
    public function update()
    {
        $user_info = $this->user_info;
        $id = Request::param('id');
        $name = Request::param('name');
        $phone = Request::param('phone');
        $province = Request::param('province');
        $city = Request::param('city');
        $county = Request::param('county');
        $detail = Request::param('detail');
        $isdefault = (int)Request::param('isdefault');
        if (!($id > 0)) {
            $this->result_error('收货信息id错误!', $id);
        }
        if (empty($name)) {
            $this->result_error("收件人不能为空");
        }
        if (empty($phone)) {
            $this->result_error("手机号不能为空");
        }
        if (empty($province)) {
            $this->result_error("省份不能为空");
        }
        if (empty($city)) {
            $this->result_error("城市不能为空");
        }
        if (empty($county)) {
            $this->result_error("市、县(区)不能为空");
        }
        if (empty($detail)) {
            $this->result_error("地址详情不能为空");
        }

        if (!preg_match('/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/', $phone)) {
            $this->result_error('手机号格式错误');
        }
        if (!preg_match('/^[\u4E00-\u9FA5A-Za-z]+$/', $name) && strlen($name) < 25) {
            $this->result_error('姓名错误');
        }
        if (!preg_match('/^[\u4E00-\u9FA5A-Za-z0-9_-]+$/', $detail) && strlen($detail) <= 60) {
            $this->result_error('详细地址错误');
        }

        if ($isdefault != 0 && $isdefault != 1) {
            $this->result_error('收货信息没有指定是否默认地址');
        }

        $up_res = Db::name('user_address')->where('id',$id)->update([
            'uid' => $user_info['id'],
            'name' => $name,
            'phone' => $phone,
            'province' => $province,
            'city' => $city,
            'county' => $county,
            'detail' => $detail,
            'isdefault' => $isdefault
        ]);
        if ($up_res) {
            $this->updateUserInfo($user_info['id']);
            $this->result_ok("更新成功");
        }
        $this->result_error("更新失败");
    }

    /** 删除收货地址 */
    public function delete()
    {
        $user_info = $this->user_info;
        $id = Request::param('id');
        if (!($id > 0)) {
            $this->result_error('收货信息id错误!', $id);
        }
        $sql_result = Db::name('user_address')->where('id', $id)->delete();
        if ($sql_result) {
            $this->updateUserInfo($user_info['id']);
            $this->result_ok("删除成功");
        }
        $this->result_ok("删除失败");
    }

}